package com.loongtech.bi.entity.POJO;

import java.io.Serializable;
import java.util.Collection;

/**
 * Author: yuri
 * Description: 分页包装类
 * Date: 17:35 2017/5/18
 */
public class Page<E extends Collection<?>> implements Serializable {
    private static final long serialVersionUID = -2603023955707872840L;

    /* 分页数据 */
    private E data;
    /* 数据总条数 */
    private int totalSize;
    /* 每页显示条数 */
    private int pageSize = DEFAULT_PAGE_SIZE;
    /* 当前页数 */
    private int curPage;
    /* 总页数 */
    private int totalPage;

    /*默认的每页记录数:20 */
    private static int DEFAULT_PAGE_SIZE = 20;

    public Page(E data, int totalSize) {
        this.data = data;
        if (totalSize >= 0) {
            this.totalSize = totalSize;
        } else {
            this.totalSize = 0;
        }
    }

    public Page(E data, int totalSize, int curPage, int pageSize) {
        this.data = data;
        if (totalSize >= 0) {
            this.totalSize = totalSize;
        } else {
            this.totalSize = 0;
        }
        if (pageSize > 0) {
            this.pageSize = pageSize;
        }
        this.curPage = curPage;
        this.totalPage = (totalSize + this.pageSize - 1) / this.pageSize;
        if (totalPage < 1) {
            totalPage = 1;
        }
    }

    public E getData() {
        return data;
    }

    public void setData(E data) {
        this.data = data;
    }

    public int getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if (pageSize > 0) {
            this.pageSize = pageSize;
            if (totalSize > 0) {
                this.totalPage = (totalSize + this.pageSize - 1) / this.pageSize;
                if (totalPage < 1) {
                    totalPage = 1;
                }
            }
        }
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{\"totalSize\":\"").append(totalSize).append("\", \"pageSize\":\"").append(pageSize)
                .append("\", \"curPage\":\"").append(curPage).append("\", \"totalPage\":\"").append(totalPage);
        if (data != null) {
            builder.append("\", \"datasize\":\"").append(data.size()).append("\"}");
        } else {
            builder.append("\"}");
        }
        return builder.toString();
    }

    public static int getDefaultPageSize() {
        return DEFAULT_PAGE_SIZE;
    }

    public static int getStartOfPage(int pageNo, int pageSize) {
        if (pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize < 1) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        return (pageNo - 1) * pageSize;
    }
}
